
package com.showcai.controller.sync;

import com.google.gson.reflect.TypeToken;
import com.showcai.controller.base.BaseController;
import com.showcai.module.DevicIccRelative;
import com.showcai.module.SyncResult;
import com.showcai.module.sync.Devic;
import com.showcai.module.sync.Member;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.service.DevicIccRelativeService;
import com.showcai.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/sync")
@Controller
public class SyncController extends BaseController {

    @Autowired
    ScheduleService scheduleService;
    @Autowired
    DevicIccRelativeService devicIccRelativeService;

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public String syncUser() {

/*        {
            "mobile": "15112292550",
                "password": "e10adc3949ba59abbe56e057f20f883e",
                "nickname": "ux464475",
                "memberId": "12",
                "avatar": "http://xxx.com/avatar.jpg"
        }*/
        SyncResult syncResult = new SyncResult(SyncResult.STATUS_SUCC, SyncResult.MSG_SUCC);
        try {
            Member member = (Member) super.json2Obj(Member.class);
            userService.synUser(member);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        } catch (Exception ex) {
            ex.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        }
        return syncResult.toJson();
    }

    @RequestMapping(value = "/iccId", method = RequestMethod.POST)
    @ResponseBody
    public String iccId() {

/*        {“devicId”:devicId,"iccId":iccid}*/
        SyncResult syncResult = new SyncResult(SyncResult.STATUS_SUCC, SyncResult.MSG_SUCC);
        try {
            DevicIccRelative devicIccRelative = (DevicIccRelative) super.json2Obj(DevicIccRelative.class);
            devicIccRelative= devicIccRelativeService.modifyDevicIccRelative(devicIccRelative);
            syncResult.setFlag(devicIccRelative.getStatus());
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        } catch (Exception ex) {
            ex.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        }
        return syncResult.toJson();
    }

    @RequestMapping(value = "/devic")
    @ResponseBody
    public String syncDevic() {
        SyncResult syncResult = new SyncResult(SyncResult.STATUS_SUCC, SyncResult.MSG_SUCC);

/*

解绑或非管理员解绑
{
	"deviceId": "12312",
	"followers": [{
			"mobile": "123",
			"isAdmin": true
		},
		{
			"mobile": "456",
			"isAdmin": false
		},
	]
}


管理员解绑
{
	"deviceId": "12312",
	"followers": []
}
        */

        try {
            Devic devic = (Devic) super.json2Obj(new TypeToken<Devic>() {
            }.getType());
            userService.synDevic(devic);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        } catch (Exception ex) {
            ex.printStackTrace();
            syncResult = new SyncResult(SyncResult.STATUS_FAIL, SyncResult.MSG_FAIL);
        }
        return syncResult.toJson();
    }


    /**
     * 获取全部日程列表
     *
     * @param devicId 设备Id
     * @return
     */
    @RequestMapping(value = "/get/scheduleList/{devicId}", method = RequestMethod.GET)
    @ResponseBody
    public String scheduleList(@PathVariable String devicId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setData(scheduleService.getScheduleListByDevicId(devicId));
        return resultObj.toJson();
    }

}
